﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Modelling.Components.Actions
{
    public enum ComponentAlign
    {
        Vertical, 
        Horizontal
    }

    public class ActionAlignSelectedShprossConnectors : IComponentAction
    {
        private ModelControl modelControl;
        private ComponentAlign componentAlign;
        public ActionAlignSelectedShprossConnectors(ModelControl modelControl, ComponentAlign align, RectangleF boundingBox)
        {
            this.modelControl = modelControl;
            this.componentAlign = align;
        }

        #region IComponentAction Members

        /// <summary>
        /// Параметр - коннектор, по которому будет выполняться выравнивание.
        /// </summary>
        /// <param name="component"></param>
        /// <returns></returns>
        public bool Action(IComponent component)
        {
            List<IComponent> componentsToAlign = new List<IComponent>();
            if (!(component is ShprossConnector))
                return false;
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
                if(selectedComponent != component && selectedComponent is ShprossConnector)
                    componentsToAlign.Add(selectedComponent);

            switch (componentAlign)
            {
                case ComponentAlign.Horizontal:
                    foreach (IComponent componentToAlign in componentsToAlign)
                    {
                        RectangleF boundingBox = componentToAlign.Parent.BoundingBox;
                        componentToAlign.MoveTo(new ModelPoint(componentToAlign.Position.X, component.Position.Y), boundingBox);
                    }
                    break;
                case ComponentAlign.Vertical:
                    foreach (IComponent componentToAlign in componentsToAlign)
                    {
                        RectangleF boundingBox = componentToAlign.Parent.BoundingBox;
                        componentToAlign.MoveTo(new ModelPoint(component.Position.X, componentToAlign.Position.Y), boundingBox);
                    }
                    break;
                default:
                    throw new NotImplementedException();
            }
            return true;
        }

        #endregion
    }
}
